package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
//import java.net.*;

import BeanManager.UserManager;
import Beans.User;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author r_uddie
 */
public class ChangePasswordServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String encryptedPassword;
        //HttpSession session = request.getSession();
        
        String email = request.getParameter("username");
        String password = request.getParameter("password");
        String newpassword = request.getParameter("newpassword");
        String confirmpassword = request.getParameter("confirmpassword");
        String changepassword = request.getParameter("changepassword");
        
        if(changepassword != null)
        {
            User user = UserManager.getUser( email );
            if ( user.getPassword().equals( UserManager.encrypt( UserManager.encrypt( password ) ) ) )
                out.println( "<h2>Congratulations " + user.getFirstName() + " !!</h2> <form method=\"POST\" action=\"ChangePasswordServlet\" ><input type=\"hidden\" name=\"username\" value=" + user.getEmailAddress() + " /><br />New Password : <input type=\"password\" name=\"newpassword\" value=\"\" /><br />Confirm Password : <input type=\"password\" name=\"confirmpassword\" value=\"\" /><br /><input type=\"submit\" value=\"Change Password\" /></form>");
            else
                out.println("Wrong Password!");
        }
        else
        {
            if(newpassword == null)
                out.println("Illegal Entry! Change Password request has not been placed.");
            else
            {
                if(newpassword.equals(confirmpassword))
                {
                    encryptedPassword = UserManager.encrypt( UserManager.encrypt( newpassword ) ) ;
                    out.println( UserManager.updateUser(email, "PASSWORD", encryptedPassword) + "<form method=\"POST\" action=\"login.jsp\" >"+
                    "<input type=\"submit\" value=\"Proceed to Login\" /></form>");
                    //session.setAttribute("user", null);
                }
                else
                    out.println("New Password was not confirmed correctly.");
            }
        }
            
        out.close();
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
